<?php

	if(isset($_POST['Pricerr_post_new_job']))
	{
		global $current_user;
		$pid = $_GET['jobid'];					//this is a new pid, after posting the post_new_page?
		
		global $adOK, $post_new_error;
		get_currentuserinfo();
		$uid = $current_user->ID;		
		
		// SpW deleted:---- job additional services -----for($k=1;$k<=3;$k++) ...	
		unset($_SESSION['i_will']);
		unset($_SESSION['price']);
		
		$the_sale				= trim(htmlspecialchars($_POST['the_sale']));
		$brand					= htmlspecialchars($_POST['brand']);
		$price					= $_POST['price'];
		$rsvp_description		= substr(nl2br(strip_tags(htmlspecialchars($_POST['rsvp_description']))), 0 , 500);
		//$job_category 		= htmlspecialchars($_POST['job_cat_cat']);
		//$job_tags 			= trim(htmlspecialchars($_POST['job_tags']));
		//$max_days 			= trim(htmlspecialchars($_POST['max_days']));
		$instruction_box		= substr( nl2br(strip_tags(htmlspecialchars($_POST['instruction_box']))), 0 , 500);
		$post_description 		= $description ." special terms  ". $instruction_box;
		
		//----------------------
		$out	= preg_split('/\s+/',trim($the_sale)); 
		$last_word = $out[count($out)-1]; 
		
		if($last_word == PricerrTheme_get_for_strg()) $the_sale = substr($the_sale, 0, strrpos($the_sale, " ")); 
		//----------------------------------------------------------------
		
		$adOK = 1;
	
		do_action('PricerrTheme_post_new_submit');
	
		//-----------------------------------
		$new_post = array();
		$new_post['post_title'] = $the_sale;
		$new_post['ID'] = $pid;
		$new_post['post_content'] = $post_description;	
		wp_update_post( $new_post );
		// wp_set_post_tags( $pid, $job_tags);     
		
		//-----------------------------------
		$pricerrTheme_variable_cost_job = get_option('pricerrTheme_variable_cost_job');
		$pricerrTheme_free_input_cost_job = get_option('pricerrTheme_free_input_cost_job');
		
		$PricerrTheme_enable_dropdown_values = get_option('PricerrTheme_enable_dropdown_values');
        $PricerrTheme_enable_free_input_box = get_option('PricerrTheme_enable_free_input_box');	
		
		if($PricerrTheme_enable_dropdown_values == "yes" || $PricerrTheme_enable_free_input_box == "yes") 
		{
			$prc = PricerrTheme_get_show_price($price);
			$prc_to_set = $price;
			
				if($PricerrTheme_enable_free_input_box == "yes")
					update_post_meta($pid, "variable_cost", '1');
					else update_post_meta($pid, "input_free_cost", '1');
		}
		else
		{		
			$prc = PricerrTheme_get_show_price(get_option('PricerrTheme_job_fixed_amount')); 
			$prc_to_set = get_option('PricerrTheme_job_fixed_amount');
			$price = $prc_to_set;
		}	
		
		/* Use the custome table spw_rsvp for "custom fields" storage */
		global $wpdb
		//$wpdb->insert('spw_rsvp', array("post_id" => $pid, "brand" => $brand, /*"location" => $latlon*/, "price"=>$price), array("%d", "%s", /*"%d"*/, "%d"));
		
		//---------------------------------------------------------	 
		if(empty($price)) 			{ $adOK = 0; $post_new_error['price']			= __('You cannot leave the job price blank!','PricerrTheme'); }
		if(empty($the_sale)) 		{ $adOK = 0; $post_new_error['the_sale'] 		= __('You cannot leave the title!','PricerrTheme'); }
		if(empty($rsvp_description)) { $adOK = 0; $post_new_error['description'] 	= __('You cannot leave the job description blank!','PricerrTheme'); }
		//if(empty($job_category)) 	{ $adOK = 0; $post_new_error['job_category'] 	= __('Please select a category for your job.','PricerrTheme'); }
		
		//if(empty($max_days)) { $adOK = 0; $post_new_error['maxdays1'] = __('Please provide max days for delivery.','PricerrTheme'); }
		//elseif(!is_numeric($max_days) || $max_days < 0) { $adOK = 0; $post_new_error['maxdays2'] = __('Max days must be a number > 0','PricerrTheme'); }
	
		//-----------------------------------------
		
		if($adOK == 1) //if everything ok, go to next step
		{		
			$the_new_post 						= array();	
			$the_new_post['post_status'] 		= 'draft';
			$the_new_post['ID'] 				= $pid;		
			wp_update_post( $the_new_post );
			
			// get the newly post, and retrieve its author's info (email address)	
			$post 		= get_post($pid);
			$author 	= get_userdata($post->post_author);
			$user_email = $author->user_email;
			
			$PricerrTheme_admin_approve_job = get_option('PricerrTheme_admin_approve_job');							
	
			if($featured == "1")   //the featured feature is not there, at the moment
			{
				$using_permalinks = PricerrTheme_using_permalinks();
				
				if($using_permalinks) $rdrlnk = get_permalink(get_option('PricerrTheme_pay_for_posting_job_page_id'))."?jobid=".$pid;
				else $rdrlnk = get_bloginfo('siteurl')."/?page_id=".get_option('PricerrTheme_pay_for_posting_job_page_id')."&jobid=".$pid;
				
				wp_redirect($rdrlnk);
			}
			else
			{
				if($PricerrTheme_admin_approve_job == "yes"):
				
					PricerrTheme_send_email_posted_job_not_approved($pid);
					update_post_meta($pid, 'under_review', "1");
								
				else:
					
					update_post_meta($pid, 'under_review', "0");					
					PricerrTheme_send_email_posted_job_approved($pid);

				endif;
			
				wp_redirect(get_permalink(get_option('PricerrTheme_my_account_page_id')));
			}
			exit;	
		}
	}
?>